﻿using System.Linq;
using System.Web.Mvc;
using E_Ticaret.Models;

namespace E_Ticaret.Controllers
{
    public class BaseController : Controller
    {
        public MvcDbDataContext Db = new MvcDbDataContext();
        public Customer CurrentCustomer;
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            ViewBag.colKategori = Db.Kategoris.OrderBy(k => k.OrderNumber).ToList();
            var customer = (Customer)Session["loginkey"];
            if (filterContext.ActionDescriptor.ActionName != "Login")
            {
                if (Session["loginkey"] == null || customer == null)
                {
                    filterContext.Result = RedirectToAction("Login", "My");
                }
                else
                {
                    CurrentCustomer = customer;
                }
            }
        }

        public IQueryable<Urun> AddFilter(DefaultFilter filtre, IQueryable<Urun> list)
        {
            if (!string.IsNullOrEmpty(filtre.SearchSreing))
            {
                //list.Where(l => l.SeoText.Contains(filtre.SearchSreing) || l.Title.Contains(filtre.SearchSreing) || l.Model.Contains(filtre.SearchSreing)).ToList
                //    ();
                list = list.Where(l => l.SeoText.Contains(filtre.SearchSreing) || l.Title.Contains(filtre.SearchSreing) || l.Model.Contains(filtre.SearchSreing));
            }
            if (filtre.Sirala == SiralamaEnum.ASC)
            {
                if (filtre.Alan == AlanEnum.Model)
                    return list.OrderBy(l => l.Model);
                if (filtre.Alan == AlanEnum.Price)
                    return list.OrderBy(l => l.Price);
                if (filtre.Alan == AlanEnum.Title)
                    return list.OrderBy(l => l.Title);
            }
            else
            {
                if (filtre.Alan == AlanEnum.Model)
                    return list.OrderByDescending(l => l.Model);
                if (filtre.Alan == AlanEnum.Price)
                    return list.OrderByDescending(l => l.Price);
                if (filtre.Alan == AlanEnum.Title)
                    return list.OrderByDescending(l => l.Title);
            }
            return list;
        }
    }
}